package com.gitee.lw114514.webapp_spring_1.service.impl;

import com.gitee.lw114514.webapp_spring_1.dao.UserDao;
import com.gitee.lw114514.webapp_spring_1.dao.impl.UserDaoImpl;
import com.gitee.lw114514.webapp_spring_1.pojo.User;
import com.gitee.lw114514.webapp_spring_1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

//@Component
@Service // 表示这个类被Spring管理
public class UserServiceImpl implements UserService {
    @Autowired //应用程序运行时，会自动查询该类型的bean对象，并注入到这个变量中
    private UserDao userDao;

    @Override
    public List<User> findAll() {
        //1.调用dao层获取用户数据
        List<String> lines =  userDao.findAll();

        List<User> userList = lines.stream().map(line -> {
            String[] parts = line.split(",");
            Integer id = Integer.parseInt(parts[0]);
            String name = parts[1];
            Integer code = Integer.parseInt(parts[2]);
            LocalDate registration_time = LocalDate.parse(parts[3],
                    DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            return new User(id, name, code, registration_time);
        }).toList();

        return userList;
    }
}
